// 引入api请求
import {
  requestLoginIn, //登录请求
  requestUsername, //获取用户名
  requestLoginOut//退出登录
} from "@/api"
import tempMark from '@/utils/tempMark'
const state = {
  userTempId: tempMark(),
  token: localStorage.getItem("user_Token") || "",
  nickName: ""
}
const mutations = {
  setToken(state, payload) {
    state.token = payload
    // 本地存储
    localStorage.setItem("user_Token", payload);
  },
  setNickName(state, payload) {
    state.nickName = payload
  },
  // 登出后清空数据
  SetLoginOut(state) {
    state.userTempId = ""
    state.token = ""
    state.nickName = ""
    localStorage.removeItem("user_Token");
  }
}
const actions = {
  // 登录并获取token
  async getLoginIn({ commit }, userInfo) {
    const res = await requestLoginIn(userInfo)
    commit("setToken", res.token)
  },
  // 获取用户名
  async getUserName({ commit }) {
    const res = await requestUsername()
    commit('setNickName', res.nickName)
  },
  async getLoginOut({ commit }) {
    await requestLoginOut()
    commit('SetLoginOut')
  }
}
const getters = {
}
// 暴漏模块 
export default {
  // 命名空间namespaced
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}